<?php
// 加密测试
namespace app\interfaces\controller;

use think\Request;
use think\Exception;
use mrmiao\encryption\RSACrypt;
use app\interfaces\model\Ad as AdModel;
use app\interfaces\model\Shop as ShopModel;
use app\interfaces\model\Member as MemberModel;

class Encryption
{
    // 测试接口  （只传值不接值）
    public function index()
    {
        // 加密类实例化
        $crypt = new RSACrypt();
        // banner列表
        $banner_list = (new AdModel)->where(['parenTid' => '8'])->field('linkUrl,picUrl,skip_type')->select();

        $shop_list = (new ShopModel)->where(['status' => '1', 'valid_time' => [['gt', time()], ['eq', ''], 'or'], 'index_recommend' => '1'])->field('member_id,logo,title,description')->order(['sort' => 'desc'])->limit(4)->select();

        return $crypt->response(
            [
                'code'        => 200,
                'message'     => 'success',
                'banner_list' => $banner_list,
                'shop_list'   => $shop_list
            ]);

    }


    // 测试接口 （接值传值）
    public function login()
    {
        if (!Request::instance()->isPost()) return ['code' => 400, 'message' => '请求方式错误'];

        // 加密类实例化
        $crypt = new RSACrypt();

        $param = $crypt->request(['username' => 'string', 'password' => 'int']);


        // 获取参数
        $username = $param['username'];
        $password = mcs($param['password']);

        if (empty($username) || empty($password)) throw new Exception('not Empty');

        $model = new MemberModel();
        // 验证数据
        $user_id = $model->where(['username' => $username, 'password' => $password])->value('id');

        if ($user_id) {
            return ['code' => 200, 'message' => '登录成功', 'data' => ['user_id' => $user_id]];
        }

        return ['code' => 401, 'message' => '账号或密码错误'];
    }
}